home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_std / real.e < prev    next >
Text File  |  1998-12-22  |  6KB  |  277 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. expanded class  REAL
  13. --
  14. -- Note: An Eiffel REAL is mapped as a C float or as a Java float.
  15. --
  16.  
  17. inherit
  18.    REAL_REF
  19.       redefine
  20.      infix "+", infix "-", infix "*", infix "/", infix "^",
  21.      prefix "+", prefix "-", divisible, infix "<", 
  22.      infix "<=", infix ">", infix ">=", compare, one, 
  23.      zero, out_in_tagged_out_memory, fill_tagged_out_memory,
  24.      hash_code
  25.       end;
  26.    
  27. feature {ANY}
  28.  
  29.    infix "+" (other: REAL): REAL is
  30.       external "SmallEiffel"
  31.       end;
  32.  
  33.    infix "-" (other: REAL): REAL is
  34.       external "SmallEiffel"
  35.       end;
  36.  
  37.    infix "*" (other: REAL): REAL is
  38.       external "SmallEiffel"
  39.       end;
  40.    
  41.    infix "/" (other: REAL): REAL is
  42.       external "SmallEiffel"
  43.       end;
  44.  
  45.    infix "^" (e: INTEGER): DOUBLE is
  46.       do
  47.      check
  48.         Current = 0.0 implies e > 0;
  49.      end;
  50.      Result := to_double ^ e; 
  51.       end;
  52.  
  53.    prefix "+" : REAL is
  54.       do
  55.      Result := Current
  56.       end;
  57.  
  58.    prefix "-": REAL is
  59.       external "SmallEiffel"
  60.       end;
  61.  
  62.    abs: REAL is
  63.       do
  64.      if Current < 0.0 then
  65.         Result := -Current;
  66.      else
  67.         Result := Current;
  68.      end;
  69.       end;
  70.          
  71.    infix "<" (other: REAL): BOOLEAN is
  72.       external "SmallEiffel"
  73.       end;
  74.    
  75.    infix "<=" (other: REAL): BOOLEAN is
  76.       external "SmallEiffel"
  77.       end;
  78.    
  79.    infix ">" (other: REAL): BOOLEAN is
  80.       external "SmallEiffel"
  81.       end;
  82.    
  83.    infix ">=" (other: REAL): BOOLEAN is
  84.       external "SmallEiffel"
  85.       end;
  86.    
  87.    compare(other: REAL): INTEGER is
  88.       do
  89.      if Current < other then
  90.         Result := -1
  91.      else
  92.         if Current > other then
  93.            Result := 1
  94.         end;
  95.      end;
  96.       end;
  97.  
  98.    divisible(other: REAL): BOOLEAN is
  99.       do
  100.      Result := (other /= 0.0)
  101.       end;
  102.    
  103.    one: REAL is 1.0;
  104.    
  105.    zero: REAL is 0.0;
  106.    
  107.    floor: INTEGER is
  108.      -- Greatest integral value no greater than Current.
  109.       do
  110.      Result := to_double.floor;
  111.       ensure 
  112.      result_no_greater: Current >= Result;
  113.      close_enough: Current - Result < one;
  114.       end;
  115.       
  116.    ceiling: INTEGER is
  117.      -- Smallest integral value no smaller than Current.
  118.       do
  119.      Result := to_double.ceiling;
  120.       ensure
  121.      result_no_smaller: Current <= Result;
  122.      close_enough: Result.to_real - Current < one;
  123.       end;
  124.  
  125.    rounded: INTEGER is
  126.            -- Rounded integral value.
  127.       do
  128.      Result := to_double.rounded;
  129.       end;
  130.  
  131.    truncated_to_integer: INTEGER is
  132.            -- Integer part (same sign, largest absolute value
  133.             -- no greater than Current).
  134.       do
  135.      Result := to_double.truncated_to_integer;
  136.       ensure
  137.      Result.to_real <= Current
  138.       end;
  139.    
  140.    to_string: STRING is
  141.      -- Convert the REAL into a new allocated STRING. 
  142.      -- Note: see `append_in' to save memory.
  143.       do
  144.      Result := to_double.to_string;
  145.       end; 
  146.  
  147.    append_in(str: STRING) is
  148.      -- Append the equivalent of `to_string' at the end of 
  149.      -- `str'. Thus you can save memory because no other
  150.      -- STRING is allocate for the job.
  151.       require
  152.      str /= Void;
  153.       do
  154.      to_double.append_in(str);
  155.       end; 
  156.    
  157.    to_string_format(d: INTEGER): STRING is
  158.      -- Convert the REAL into a new allocated STRING including 
  159.      -- only `d' digits in fractionnal part. 
  160.      -- Note: see `append_in_format' to save memory.
  161.       do
  162.      Result := to_double.to_string_format(d);
  163.       end; 
  164.  
  165.    append_in_format(str: STRING; f: INTEGER) is
  166.      -- Same as `append_in' but produce only `f' digit of 
  167.      -- the fractionnal part.
  168.       require
  169.      str /= Void;
  170.      f >= 0
  171.       do
  172.      to_double.append_in_format(str,f);
  173.       end; 
  174.    
  175.    to_double: DOUBLE is
  176.       external "SmallEiffel"
  177.       end;
  178.    
  179. feature -- Maths functions :
  180.  
  181.    sqrt: DOUBLE is
  182.            -- Compute the square routine.
  183.       require
  184.      Current >= 0
  185.       do
  186.      Result := to_double.sqrt;
  187.       end;
  188.    
  189.    sin: DOUBLE is
  190.      -- Sinus (ANSI C sin).
  191.       do
  192.      Result := to_double.sin;
  193.       end;
  194.  
  195.    cos: DOUBLE is
  196.      -- Cosinus (ANSI C cos).
  197.       do
  198.      Result := to_double.cos;
  199.       end;
  200.  
  201.    tan: DOUBLE is
  202.      -- (ANSI C tan).
  203.       do
  204.      Result := to_double.tan;
  205.       end;
  206.  
  207.    asin: DOUBLE is
  208.      -- (ANSI C asin).
  209.       do
  210.      Result := to_double.asin;
  211.       end;
  212.  
  213.    acos: DOUBLE is
  214.      -- (ANSI C acos).
  215.       do
  216.      Result := to_double.acos;
  217.       end;
  218.  
  219.    atan: DOUBLE is
  220.      -- (ANSI C atan).
  221.       do
  222.      Result := to_double.atan;
  223.       end;
  224.  
  225.    sinh: DOUBLE is
  226.      -- (ANSI C sinh).
  227.       do
  228.      Result := to_double.sinh;
  229.       end;
  230.  
  231.    cosh: DOUBLE is
  232.      -- (ANSI C cosh).
  233.       do
  234.      Result := to_double.cosh;
  235.       end;
  236.  
  237.    tanh: DOUBLE is
  238.      -- (ANSI C tanh).
  239.       do
  240.      Result := to_double.tanh;
  241.       end;
  242.  
  243.    exp: DOUBLE is
  244.      -- (ANSI C exp).
  245.       do
  246.      Result := to_double.exp;
  247.       end;
  248.  
  249.    log: DOUBLE is
  250.      -- (ANSI C log).
  251.       do
  252.      Result := to_double.log;
  253.       end;
  254.  
  255.    log10: DOUBLE is
  256.      -- (ANSI C log10).
  257.       do
  258.      Result := to_double.log10;
  259.       end;
  260.  
  261. feature -- Object Printing :
  262.  
  263.    out_in_tagged_out_memory, fill_tagged_out_memory is
  264.       do
  265.      Current.append_in(tagged_out_memory);
  266.       end;
  267.  
  268. feature -- Hashing :
  269.  
  270.    hash_code: INTEGER is
  271.       do
  272.      Result := to_double.hash_code;
  273.       end;
  274.  
  275. end --  REAL
  276.  
  277.